﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server">

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
    <div class="col-sm-12">
                <div class="ibox float-e-margins border-bottom">
                    <div class="ibox-title">
                        <h5>API参数 <small>包括控制器、方法名、参数(可选)</small></h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-down"></i>
                            </a>
                           
                        </div>
                    </div>
                    <div class="ibox-content" >
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Controller</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control controller" maxlength="30">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">Action</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control action" maxlength="30" > 
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">参数</label>
                                <div class="col-sm-10">
                                    <textarea rows="3"  class="form-control params"></textarea>
                                    <a class="help-block m-b-none check-json" style="cursor:pointer" title="校验">校验json格式</a>
                                </div>
                            </div>
                              <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                   <div class="btns-container"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
             <div class="ibox float-e-margins border-bottom">
                    <div class="ibox-title">
                        <h5>调用结果</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-down"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content api-msg" style="display: none;">
                       <textarea id="msg" spellcheck="false" class="form-control msg" rows="18"></textarea>
                    </div>
                </div>
            </div>
           
     
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
    <script type="application/javascript">
        $(function () {
            //加载按钮
            var ModuleID = Y.getUrlParam("ModuleID");
            commonUtil.loadBtnList(ModuleID, function () {
            });
            
            $('.btns-container').on('click', '#btn-search', function () {
                $(".msg").empty();
                var contoller = $(".controller").val();
                var action = $(".action").val();
                var params = $(".params").val();
                if (contoller == "") {
                    CreateMsg("Controller 不能为空.", false);
                    return false;
                }
                if (action == "") {
                    CreateMsg("Action 不能为空.", false);
                    return false;
                }
                
                Y.API2(contoller, action, params == "" ? {} : JSON.parse(params), function (d) {
                    if (d.Success == false) {
                        CreateMsg("调用错误:" + d.Message, false);
                    } else {
                        CreateMsg(d,true);
                    }
                })
            });
            $(".check-json").on("click", function () {
                var params = $(".params").val();
                CreateMsg(params,true);
            })
            
            function CreateMsg(msg,ch) {
                var objUl = $("#msg");
                objUl.val("");
                var strli="";
                if (msg == "")
                    strli = "什么都没有...";
                else {
                    if (ch)
                        strli = formatJson(msg, { newlineAfterColonIfBeforeBraceOrBracket: false, spaceAfterColon: false });
                    else
                        strli = msg;
                }
                objUl.val(strli);
                $(".api-msg").css("display", "block");
            }
        })
        
        function formatJson(json, options) {
            var reg = null,
                formatted = '',
                pad = 0,
                PADDING = '    '; // one can also use '\t' or a different number of spaces

            // optional settings
            options = options || {};
            // remove newline where '{' or '[' follows ':'
            options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
            // use a space after a colon
            options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;

            // begin formatting...
            if (typeof json !== 'string') {
                // make sure we start with the JSON as a string
                json = JSON.stringify(json);
            } else {
                // is already a string, so parse and re-stringify in order to remove extra whitespace
                json = JSON.parse(json);
                json = JSON.stringify(json);
            }

            // add newline before and after curly braces
            reg = /([\{\}])/g;
            json = json.replace(reg, '\r\n$1\r\n');

            // add newline before and after square brackets
            reg = /([\[\]])/g;
            json = json.replace(reg, '\r\n$1\r\n');

            // add newline after comma
            reg = /(\,)/g;
            json = json.replace(reg, '$1\r\n');

            // remove multiple newlines
            reg = /(\r\n\r\n)/g;
            json = json.replace(reg, '\r\n');

            // remove newlines before commas
            reg = /\r\n\,/g;
            json = json.replace(reg, ',');

            // optional formatting...
            if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
                reg = /\:\r\n\{/g;
                json = json.replace(reg, ':{');
                reg = /\:\r\n\[/g;
                json = json.replace(reg, ':[');
            }
            if (options.spaceAfterColon) {
                reg = /\:/g;
                json = json.replace(reg, ': ');
            }

            $.each(json.split('\r\n'), function (index, node) {
                var i = 0,
                    indent = 0,
                    padding = '';

                if (node.match(/\{$/) || node.match(/\[$/)) {
                    indent = 1;
                } else if (node.match(/\}/) || node.match(/\]/)) {
                    if (pad !== 0) {
                        pad -= 1;
                    }
                } else {
                    indent = 0;
                }

                for (i = 0; i < pad; i++) {
                    padding += PADDING;
                }

                formatted += padding + node + '\r\n';
                pad += indent;
            });
            formatted = formatted.replace(/^\s+/, '');
            return formatted;
        }

    </script>
</asp:Content>

